/*
 * Copyright 2006-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package asphalt2.processor
{
	import asphalt2.control.IControl;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import mx.core.ClassFactory;
	import mx.core.IFlexDisplayObject;
	import mx.managers.PopUpManager;
	/**
	 * ポップアップを上げるProcessor
	 * @author:c9katayama
	 */
	public class PopupOpenProcessor implements IProcessor
	{
		private var _popupParent:DisplayObject;
		private var _control:IControl;
		private var _popup:Class;
		private var _modal:Boolean;
		private var _parameter:Object;
		private var _listenerList:Array;
		/**
		 * PopUpを上げます。
		 * @param popupParent popupの親 ControlクラスかDisplayObject
		 * @param popup popupさせるPageクラス
		 * @param modal モーダルかどうか
		 * @param parameter ポップアップインスタンスに初期セットするパラメータ 
		 */
		public function PopupOpenProcessor(control:IControl,popup:Class,popupParent:DisplayObject=null,
				modal:Boolean=true,parameter:Object=null){
					
			if(popupParent==null){
				this._popupParent = control.UI;
			}else{
				this._popupParent = popupParent;
			}
			this._popup = popup;
			this._modal = modal;
			this._parameter = parameter;
		}
		public function get control():IControl{
			return _control;
		}
		/**
		 * ポップアップインスタンスにセットするリスナー登録
		 */
		public function addPopUpEventListener(type:String,listener:Function):void{
			if(_listenerList == null){
				_listenerList = [];				
			} 			
			_listenerList[_listenerList.length]=
				{type:type,listener:listener};			
		}
		public function get popupParent():DisplayObject{
			return _popupParent;
		}		
		public function get popup():Class{
			return _popup;
		}
		
		public function get modal():Boolean{
			return _modal;
		}

		public function get parameter():Boolean{
			return _parameter;
		}
		
		public function getHandler():Function{
			return handle;
		}
		
		public function handle(event:Event=null):void{
			openPopup();
		}
			
		public function openPopup():void{
			var popupInstance:IFlexDisplayObject = PopUpManager.createPopUp(_popupParent,_popup,_modal);
			if(_parameter != null){
				for(var key:String in _parameter){
					popupInstance[key] = _parameter[key];
				}
			}
			if(_listenerList != null){
				for(var i:Number = 0;i < _listenerList.length;i++){
					var listener:Object = _listenerList[i];
					EventDispatcher(popupInstance).addEventListener(listener.type,listener.listener);						
				}
			}
			PopUpManager.centerPopUp(popupInstance);
		}
	}
}